package com.zzw.paymentdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzw.paymentdemo.entity.OrderInfo;
import com.zzw.paymentdemo.entity.Product;
import com.zzw.paymentdemo.enums.OrderStatus;
import com.zzw.paymentdemo.mapper.OrderInfoMapper;
import com.zzw.paymentdemo.mapper.ProductMapper;
import com.zzw.paymentdemo.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.paymentdemo.util.NoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
@Slf4j
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Autowired
    ProductMapper productMapper;
    @Override
    public OrderInfo createOrderByProductId(Long productId) {
//       查找已存在但未支付的订单
        OrderInfo orderInfo=this.findNoPayByProductId(productId);

        if(orderInfo!=null){
            return orderInfo;
        }

//        根据id查找商品
        Product product = productMapper.selectById(productId);
        orderInfo=new OrderInfo();
        orderInfo.setTitle(product.getTitle());
        orderInfo.setProductId(productId);
        orderInfo.setOrderNo(NoUtils.getOrderNo()); //订单号
        orderInfo.setTotalFee(product.getPrice()); //总金额,单位为分
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());  //订单状态
//        生成订单
        baseMapper.insert(orderInfo);
        return orderInfo;
    }
    /**
     * 按时间倒序显示订单列表
     * @author zzw
     * @date 2023/5/12 17:42
     * @return java.util.List<com.zzw.paymentdemo.entity.OrderInfo>
     */
    @Override
    public List<OrderInfo> listOrderByCreateTimeDesc() {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<OrderInfo> list = baseMapper.selectList(queryWrapper);
        return list;
    }
    /**
     *更新订单状态
     * @param orderNo
     * @param orderStatus
     **/
    @Override
    public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {
        log.info("更新订单状态===》{}",orderStatus.getType());
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        orderInfo.setOrderStatus(orderStatus.getType());
        baseMapper.updateById(orderInfo);
    }
    /**
     *获取订单状态
     * @param orderNo
     * @return java.lang.String
     **/
    @Override
    public String getOrderStatus(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        //防止被删除的订单的回调通知的调用
        if(orderInfo == null){
            return null;
        }
        return orderInfo.getOrderStatus();
    }


    /**
     * 找出创建超过minutes分钟并且未支付的订单
     * @param minutes
     * @return
     */
    @Override
    public List<OrderInfo> getNoPayOrderByDuration(int minutes) {
//        minutes分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));

        QueryWrapper<OrderInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());
        queryWrapper.le("create_time",instant);
        return  baseMapper.selectList(queryWrapper);

    }

    /**
     * 根据商品id查找未支付订单
     * 防止生成多个重复订单
     * @author zzw
     * @date 2023/5/12 16:56
     * @return com.zzw.paymentdemo.entity.OrderInfo
     */
    private OrderInfo findNoPayByProductId(Long productId) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());
//        queryWrapper.eq("user_id",UserId);
        return baseMapper.selectOne(queryWrapper);

    }


    /**
     * 根据订单号获取订单
     * @param orderNo
     * @return
     */
    @Override
    public OrderInfo getOrderByOrderNo(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        return orderInfo;
    }

}
